define(function(require, exports){
	var pubjs	= require('pubjs');
	var view	= require('@base/view');
	var util	= require('util');
	var trigger	= require('modules/trigger');
	var $ 		= require('jquery');

	var Base = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'tplFile':	'admin/tpl/category/import.tpl',
				'upload_config': 'category',
				'nav': 'category',
				'view_model': {
					'headTitle': '上传文件导入'
					,'data': ''
				},
				urlImport: 'category/fileImport'
			});

			this.$id = null;
			this.Super('init', arguments);
		}
		,afterBuild:function(){
			var self = this;

			var c = self.getConfig();

/*			var param = $.extend({}, pubjs.config('uploader/' + c.upload_config), {
				'subModule': 'category/upload/uploader.base',
				'hasProgress': false
			});*/

			var config = {
				// 表单取消按钮
				'cancel': {
					'value': LANG('取消'),
					'class': 'btn btn-link mr20'
				}
				// 表单保存按钮
				,'save': {
					'value': LANG('导入'),
					'class': 'btn btn-confirm'
				}
				/*
				,type: {
					'options': pubjs.config('categories') || []
					,'key': 'id'
					,'name': 'name'
					,"def": LANG("选择对应的分类(Type)")
				}
				,channel: {
					'options': pubjs.config('exchange') || []
					,'key': 'id'
					,'name': 'name'
					,"def": LANG("选择对应的渠道(SourceId)")
				}
				*/
				//,uploader: param
			};

			self.buildFromTemplate(config);
		}
		,load: function(id){
			var self = this;
			self.$id = id;
			if(id){
				self.vm.$.isEdit = true;
				pubjs.loading.show();
				pubjs.mc.send(this.getConfig('urlInfo'), {id: id}, function(err, data){
					pubjs.loading.hide();
					if(err){
						pubjs.alert(err.message);
						return false;
					}
					self.setData(data);
				})
			}

			return self;
		}
		,validate: function(data){
			if(!data.File && !data.Content){
				pubjs.alert(LANG('请上传文件, 或填上相应的内容'));
				return false;
			}
			return true;
		}
		,getData: function(){
			var self = this
				,data;

			//var ch = self.$;
			//var file = ch.uploader.getValue();
			data = {
				//File: file && file[0]
				Content: self.vm.getItem('data')
			};

			return data;
		}
		,reset: function(){
			var ch = this.$;
			//ch.uploader.reset();
			this.vm.reset();
			//ch.type.reset();
			//ch.channel.reset();
			return this;
		}
		,save: function() {
			var self = this;
			var data = self.getData();
			if(self.validate(data)) {
				pubjs.loading.show();
				pubjs.mc.send(self.getConfig('urlImport'), data, self.afterSave.bind(self))
			}

			return false;
		}
		,afterSave: function(err, data) {
			pubjs.loading.hide();
			if(err) {
				pubjs.alert(err.message);
				return false;
			}

			pubjs.controller.navigate(this.getConfig('nav') || 'category');
		}
		,onInputClick: function(ev) {
			if(ev.name == 'save') {
				this.save();
			}
			else if(ev.name == 'cancel') {
				pubjs.controller.navigate('category');
			}
		}
	});

	exports.base = Base;

	var Batch = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'tplFile':	'admin/tpl/category/batchImport.tpl',
				'view_model': {
					'headTitle': '批量导入内部分类数据'
				},
				urlImport: 'category/loadAdx'
			});

			this.$id = null;
			this.Super('init', arguments);
		}
		,afterBuild:function(){
			var self = this;

			var config = {
				// 表单取消按钮
				'cancel': {
					'value': LANG('取消'),
					'class': 'btn btn-link mr20'
				}
				// 表单保存按钮
				,'save': {
					'value': LANG('导入'),
					'class': 'btn btn-confirm'
				}
				,type: {
					'items': [
						{id: 'app', name: LANG('APP分类')}
						,{id: 'channel', name: LANG('频道分类')}
						,{id: 'industry', name: LANG('行业分类')}
						,{id: 'media', name: LANG('媒体分类')}
						,{id: 'sensitive', name: LANG('敏感分类')}
						,{id: 'spot', name: LANG('广告(网站)分类')}
						,{id: 'video', name: LANG('视频分类')}
					]
					,hasSearch: true
					,hasOperator: true
				}
				,channel: {
					'items': pubjs.config('exchange')
					,hasSearch: true
					,hasOperator: true
				}
			};

			self.buildFromTemplate(config);
		}
		,validate: function(data){
			var self = this;

			if(!data.Type || !data.Type.length){
				pubjs.alert(LANG('请选择相应的分类.'));
				return false;
			}
			if(!data.ChannelIds || !data.ChannelIds.length){
				pubjs.alert(LANG('请选择相应的分类.'));
				return false;
			}
			return true;
		}
		,getData: function(){
			var self = this
				,data;

			var ch = self.$;
			data = {
				Type: ch.type.getValue()
				,ChannelIds: ch.channel.getValue()
			};

			return data;
		}
		,reset: function(){
			this.$.type.reset();
			this.$.channel.reset();
			return this;
		}
		,save: function() {
			var self = this;
			var data = self.getData();
			if(self.validate(data)) {
				pubjs.loading.show();
				pubjs.mc.send(self.getConfig('urlImport'), data, self.afterSave.bind(self))
			}

			return false;
		}
		,afterSave: function(err, data) {
			pubjs.loading.hide();
			if(err) {
				pubjs.alert(err.message);
				return false;
			}

			pubjs.alert(data && data.message);
		}
		,onInputClick: function(ev) {
			if(ev.name == 'save') {
				this.save();
			}
			else if(ev.name == 'cancel') {
				pubjs.controller.navigate('category');
			}
		}
	});

	exports.batch = Batch;

});